<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>02-CSS属性：背景属性</title>
  
    
      <meta 
        property="og:title" 
        content="02-CSS属性：背景属性">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/02-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2002-01-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#background-%E7%9A%84%E5%B8%B8%E8%A7%81%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7"><span class="toc-text">background 的常见背景属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-color%EF%BC%9A%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2%E7%9A%84%E8%A1%A8%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-text">background-color：背景颜色的表示方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%A8%E8%8B%B1%E8%AF%AD%E5%8D%95%E8%AF%8D%E8%A1%A8%E7%A4%BA"><span class="toc-text">用英语单词表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGB-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGB 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGBA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGBA 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">十六进制表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HSLA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">HSLA 表示法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-repeat%E5%B1%9E%E6%80%A7"><span class="toc-text">background-repeat属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-position%E5%B1%9E%E6%80%A7"><span class="toc-text">background-position属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-attachment-%E5%B1%9E%E6%80%A7"><span class="toc-text">background-attachment 属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#background-%E7%BB%BC%E5%90%88%E5%B1%9E%E6%80%A7"><span class="toc-text">background 综合属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-size%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B0%BA%E5%AF%B8"><span class="toc-text">background-size属性：背景尺寸</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%83%8C%E6%99%AF%E5%8E%9F%E7%82%B9%EF%BC%9Abackground-origin-%E5%B1%9E%E6%80%A7"><span class="toc-text">背景原点：background-origin 属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-clip%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E7%9A%84%E8%83%8C%E6%99%AF%EF%BC%88%E8%83%8C%E6%99%AF%E5%9B%BE%E7%89%87%E6%88%96%E9%A2%9C%E8%89%B2%EF%BC%89%E6%98%AF%E5%90%A6%E5%BB%B6%E4%BC%B8%E5%88%B0%E8%BE%B9%E6%A1%86%E4%B8%8B%E9%9D%A2"><span class="toc-text">background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E8%AE%BE%E7%BD%AE%E5%A4%9A%E4%B8%AA%E8%83%8C%E6%99%AF"><span class="toc-text">同时设置多个背景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B8%90%E5%8F%98%EF%BC%9Abackground-image"><span class="toc-text">渐变：background-image</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BA%BF%E6%80%A7%E6%B8%90%E5%8F%98"><span class="toc-text">线性渐变</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%84%E5%90%91%E6%B8%90%E5%8F%98"><span class="toc-text">径向渐变</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#clip-path%EF%BC%9A%E8%A3%81%E5%89%AA%E5%87%BA%E5%85%83%E7%B4%A0%E7%9A%84%E9%83%A8%E5%88%86%E5%8C%BA%E5%9F%9F%E5%81%9A%E5%B1%95%E7%A4%BA"><span class="toc-text">clip-path：裁剪出元素的部分区域做展示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      02-CSS属性：背景属性
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2001-12-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2002-01-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          CSS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>4.9k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            CSS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="background-的常见背景属性"><a href="#background-的常见背景属性" class="headerlink" title="background 的常见背景属性"></a>background 的常见背景属性</h2><p><strong>css2.1</strong> 中，常见的背景属性有以下几种：（经常用到，要记住）</p>
<ul>
<li><p><code>background-color:#ff99ff;</code>  设置元素的背景颜色。</p>
</li>
<li><p><code>background-image:url(images/2.gif);</code> 将图像设置为背景。</p>
</li>
<li><p><code>background-repeat: no-repeat;</code>  设置背景图片是否重复及如何重复，默认平铺满。（重要）</p>
<ul>
<li><code>no-repeat</code>不要平铺；</li>
<li><code>repeat-x</code>横向平铺；</li>
<li><code>repeat-y</code>纵向平铺。</li>
</ul>
</li>
<li><p><code>background-position:center top;</code> 设置背景图片在当前容器中的位置。</p>
</li>
<li><p><code>background-attachment:scroll;</code> 设置背景图片是否跟着滚动条一起移动。<br>属性值可以是：<code>scroll</code>（与fixed属性相反，默认属性）、<code>fixed</code>（背景就会被固定住，不会被滚动条滚走）。</p>
</li>
<li><p>另外还有一个综合属性叫做<code>background</code>，它的作用是：将上面的多个属性写在一个声明中。</p>
</li>
</ul>
<p><strong>CSS3</strong> 中，新增了一些background属性：</p>
<ul>
<li><p>background-origin</p>
</li>
<li><p>background-clip 背景裁切</p>
</li>
<li><p>background-size 调整尺寸</p>
</li>
<li><p>多重背景</p>
</li>
</ul>
<p>上面这几个属性经常用到，需要记住。现在我们逐个进行讲解。</p>
<h2 id="background-color：背景颜色的表示方法"><a href="#background-color：背景颜色的表示方法" class="headerlink" title="background-color：背景颜色的表示方法"></a>background-color：背景颜色的表示方法</h2><p>css2.1 中，颜色的表示方法有三种：单词、rgb表示法、十六进制表示法。</p>
<p>比如红色可以有下面的三种表示方法：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>: red;</span><br><span class="line"><span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>);</span><br><span class="line"><span class="attribute">background-color</span>: <span class="number">#ff0000</span>;</span><br></pre>

<p>CSS3 中，有一种新的表示颜色的方式：RGBA或者HSLA。</p>
<p>RGBA、HSLA可应用于<strong>所有</strong>使用颜色的地方。</p>
<p>下面分别介绍。</p>
<h3 id="用英语单词表示"><a href="#用英语单词表示" class="headerlink" title="用英语单词表示"></a>用英语单词表示</h3><p>能够用英语单词来表述的颜色，都是简单颜色，比如red、green、blue、orange、gray等。代码举例：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>: red;</span><br></pre>
<h3 id="RGB-表示法"><a href="#RGB-表示法" class="headerlink" title="RGB 表示法"></a>RGB 表示法</h3><p>RGB 表示三原色“红”red、“绿”green、“蓝”blue。</p>
<p>光学显示器中，每个像素都是由三原色的发光原件组成的，靠明亮度不同调成不同的颜色的。r、g、b的值，每个值的取值范围0~255，一共256个值。</p>
<p>比如红色：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>);</span><br></pre>

<p>黑色：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>);</span><br></pre>

<p>颜色可以叠加，比如黄色就是红色和绿色的叠加：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">255</span>,<span class="number">255</span>,<span class="number">0</span>);</span><br></pre>

<h3 id="RGBA-表示法"><a href="#RGBA-表示法" class="headerlink" title="RGBA 表示法"></a>RGBA 表示法</h3><pre class="highlight"><span class="line">background-<span class="attr">color</span>: <span class="title function_">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">255</span>, <span class="number">0.3</span>);</span><br><span class="line"></span><br><span class="line"><span class="attr">border</span>: 30px solid <span class="title function_">rgba</span>(<span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>, <span class="number">0.3</span>);</span><br></pre>

<p><strong>代码解释</strong>：</p>
<ul>
<li><p>RGBA 即：Red 红、Green 绿、Blue 蓝、Alpha 透明度。</p>
</li>
<li><p>R、G、B 的取值范围是：0<del>255；透明度的取值范围是 0</del>1。</p>
</li>
</ul>
<p><strong>RGB色彩模式：</strong></p>
<ul>
<li>自然界中绝大部分颜色都可以用红、绿、蓝(RGB)这三种颜色波长的不同强度组合而得，这就是人们常说的三原色原理。</li>
<li>RGB三原色也叫加色模式，这是因为当我们把不同光的波长加到一起的时候，可以得到不同的混合色。例：红+绿&#x3D;黄色，红+蓝＝紫色，绿+蓝&#x3D;青。</li>
<li>RGB各有256级(0-255)亮度，256级的RGB色彩总共能组合出约1678万种色彩，即256×256×256&#x3D;16777216。</li>
</ul>
<p> 在数字视频中，对RGB三基色各进行8位编码就构成了大约1678万种颜色，这就是我们常说的真彩色。所有显示设备都采用的是RGB色彩模式。</p>
<h3 id="十六进制表示法"><a href="#十六进制表示法" class="headerlink" title="十六进制表示法"></a>十六进制表示法</h3><p>比如红色：</p>
<pre class="highlight"><span class="line">background-color: #ff0000;</span><br></pre>

<p>所有用<code>#</code>开头的色值，都是16进制的。</p>
<p>这里，我们就要学会16进制与10进制之间的转换。下面举几个例子。</p>
<p>问：16进制中的28等于10进制的多少？<br>答：2*16+8 &#x3D; 40。</p>
<p>16进制中的af等于10进制的多少？<br>答：10 * 16 + 15 &#x3D; 175</p>
<p>以此类推：</p>
<ul>
<li><p>#ff0000等于rgb(255,0,0)。</p>
</li>
<li><p><code>background-color: #123456;</code>等价于<code>background-color: rgb(18,52,86);</code></p>
</li>
</ul>
<p><strong>十六进制可以简化为3位，所有#aabbcc的形式，能够简化为#abc</strong>。举例如下：</p>
<p>比如：</p>
<pre class="highlight"><span class="line">background-color:#ff0000;</span><br></pre>

<p>等价于：</p>
<pre class="highlight"><span class="line">background-color:#f00;</span><br></pre>

<p>比如：</p>
<pre class="highlight"><span class="line">background-color:#112233;</span><br></pre>

<p>等价于：</p>
<pre class="highlight"><span class="line">background-color:#123;</span><br></pre>

<p>但是，比如下面这个是无法简化的：</p>
<pre class="highlight"><span class="line">background-color:#222333;</span><br></pre>

<p>再比如，下面这个也是无法简化的：</p>
<pre class="highlight"><span class="line">background-color:#123123;</span><br></pre>

<p>几种常见的颜色简写可以记住。如下：</p>
<pre class="highlight"><span class="line">#000   黑</span><br><span class="line">#fff   白</span><br><span class="line">#f00   红</span><br><span class="line">#222   深灰</span><br><span class="line">#333   灰</span><br><span class="line">#ccc   浅灰</span><br></pre>

<h3 id="HSLA-表示法"><a href="#HSLA-表示法" class="headerlink" title="HSLA 表示法"></a>HSLA 表示法</h3><p>举例：</p>
<pre class="highlight"><span class="line">background-<span class="attr">color</span>: <span class="title function_">hsla</span>(<span class="number">240</span>,<span class="number">50</span>%,<span class="number">50</span>%,<span class="number">0.4</span>);</span><br></pre>

<p>解释：</p>
<ul>
<li><p><code>H</code> 色调，取值范围 0~360。0或360表示红色、120表示绿色、240表示蓝色。</p>
</li>
<li><p><code>S</code> 饱和度，取值范围 0%~100%。值越大，越鲜艳。</p>
</li>
<li><p><code>L</code> 亮度，取值范围 0%~100%。亮度最大时为白色，最小时为黑色。</p>
</li>
<li><p><code>A</code> 透明度，取值范围 0~1。</p>
</li>
</ul>
<p>如果不知道 H 的值该设置多少，我们不妨来看一下<strong>色盘</strong>：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_1545.png" class="lozad post-image"src="http://img.smyhvae.com/20180207_1545.png"></p>
<p>推荐链接：<a target="_blank" rel="noopener" href="http://www.uisdc.com/how-to-create-color-palettes">配色宝典</a></p>
<p><strong>关于设置透明度的其他方式：</strong></p>
<p>（1）<code>opacity: 0.3;</code> 会将整个盒子及子盒子设置透明度。也就是说，当盒子设置半透明的时候，会影响里面的子盒子。</p>
<p>（2）<code>background: transparent;</code> 可以单独设置透明度，但设置的是完全透明（不可调节透明度）。</p>
<h2 id="background-repeat属性"><a href="#background-repeat属性" class="headerlink" title="background-repeat属性"></a><code>background-repeat</code>属性</h2><p><code>background-repeat:no-repeat;</code>设置背景图片是否重复及如何重复，默认平铺满。属性值可以是：</p>
<ul>
<li><code>no-repeat</code>（不要平铺）</li>
<li><code>repeat-x</code>（横向平铺）</li>
<li><code>repeat-y</code>（纵向平铺）</li>
</ul>
<p>这个属性在开发的时候也是经常用到的。我们通过设置不同的属性值来看一下效果吧：</p>
<p>（1）不加这个属性时：（即默认时）（背景图片会被平铺满）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-19.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-19.png"></p>
<p>PS：padding的区域也是有背景图的。</p>
<p>（2）属性值为<code>no-repeat</code>（不要平铺）时：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-20.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-20.png"></p>
<p>（3）属性值为<code>repeat-x</code>（横向平铺）时：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-21.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-21.png"></p>
<p>其实这种属性的作用还是很广的。举个例子，设计师设计一张宽度只有1px、颜色纵向渐变的图片，然后我们通过这个属性将其进行水平方向的平铺，就可以看到整个页面都是渐变的了。</p>
<p>在搜索引擎上搜“<strong>平铺背景</strong>”，就可以发现，<strong>周期性的图片</strong>可以采用此种方法进行平铺。</p>
<p>（4）属性值为<code>repeat-y</code>（纵向平铺）时：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-22.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-22.png"></p>
<h2 id="background-position属性"><a href="#background-position属性" class="headerlink" title="background-position属性"></a><code>background-position</code>属性</h2><p><code>background-position</code>属性指的是<strong>背景定位</strong>属性。公式如下：</p>
<p>在描述属性值的时候，有两种方式：用像素描述、用单词描述。下面分别介绍。</p>
<p><strong>1、用像素值描述属性值：</strong></p>
<p>格式如下：</p>
<pre class="highlight"><span class="line">background-position:向右偏移量 向下偏移量;</span><br></pre>

<p>属性值可以是正数，也可以是负数。比如：<code>100px 200px</code>、<code>-50px -120px</code>。</p>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170812_1643.png" class="lozad post-image"src="http://img.smyhvae.com/20170812_1643.png"></p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170812_1645.png" class="lozad post-image"src="http://img.smyhvae.com/20170812_1645.png"></p>
<p><strong>2、用单词描述属性值：</strong></p>
<p>格式如下：</p>
<pre class="highlight"><span class="line">background-position: 描述左右的词 描述上下的词;</span><br></pre>

<ul>
<li>描述左右的词：left、center、right</li>
<li>描述上下的词：top 、center、bottom</li>
</ul>
<p>比如说，<code>right center</code>表示将图片放到右边的中间；<code>center center</code>表示将图片放到正中间。</p>
<p>比如说，<code>bottom</code>表示图片的底边和父亲<strong>底边贴齐</strong>（好好理解）。</p>
<p>位置属性有很多使用场景的。我们来举两个例子。</p>
<p>场景1：（大背景图）</p>
<p>打开“暗黑3 台湾”的官网<a target="_blank" rel="noopener" href="https://tw.battle.net/d3/zh/">https://tw.battle.net/d3/zh/</a>，可以看到官网的效果是比较炫的：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170812_1945.jpg" class="lozad post-image"src="http://img.smyhvae.com/20170812_1945.jpg"></p>
<p>检查网页后，找到网站背景图片的url：<a target="_blank" rel="noopener" href="https://tw.battle.net/d3/static/images/layout/bg-repeat.jpg">https://tw.battle.net/d3/static/images/layout/bg-repeat.jpg</a>。背景图如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170812_1950.jpg" class="lozad post-image"src="http://img.smyhvae.com/20170812_1950.jpg"></p>
<p>实际上，我们是通过把这张图片作为网站的背景图来达到显示效果的。只需要给body标签加如下属性即可：</p>
<pre class="highlight"><span class="line">body&#123;</span><br><span class="line">    background-image: url(/Users/smyhvae/Dropbox/img/20170812_1950.jpg);</span><br><span class="line">    background-repeat: no-repeat;</span><br><span class="line">    background-position: center top;</span><br><span class="line">&#125;</span><br></pre>

<p>上方代码中，如果没加<code>background-position</code>这个属性，背景图会默认处于浏览器的左上角（显得很丑）；加了此属性之后，图片在水平方向就位于浏览器的中间了。</p>
<p>场景2：（通栏banner）</p>
<p>很多网站的首页都会有banner图（网站最上方的全屏大图叫做「<strong>通栏banner</strong>」），这种图要求横向的宽度特别大。比如说，设计师给你一张1920*465的超大banner图，如果我们把这个banner图作为img标签直接插入网页中，会有问题的：首先，图片不在网页的中间；其次，肯定会出现横向滚动条。如下图所示：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170813_1102.gif" class="lozad post-image"src="http://img.smyhvae.com/20170813_1102.gif"></p>
<p>正确的做法是，将banner图作为div的背景图，这样的话，背景图超出div的部分，会自动移溢出。需要给div设置的属性如下：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">465px</span>;</span><br><span class="line">    <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">http://img.smyhvae.com/20170813_1053.jpg</span>);</span><br><span class="line">    <span class="attribute">background-position</span>: center top;</span><br><span class="line">    <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line">&#125;</span><br></pre>

<p>上方代码中，我们给div设置height（高度为banner图的高度），不需要设置宽度（因为宽度会自动霸占整行）。效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170813_1119.gif" class="lozad post-image"src="http://img.smyhvae.com/20170813_1119.gif"></p>
<p>上图可以看出，将banner图作为div的背景后，banner图会永远处于网页的正中间（水平方向来看）。</p>
<h2 id="background-attachment-属性"><a href="#background-attachment-属性" class="headerlink" title="background-attachment 属性"></a>background-attachment 属性</h2><ul>
<li><code>background-attachment:scroll;</code> 设置背景图片是否固定。属性值可以是：<ul>
<li><code>fixed</code>（背景就会被固定住，不会被滚动条滚走）。</li>
<li><code>scroll</code>（与fixed属性相反，默认属性）</li>
</ul>
</li>
</ul>
<p><code>background-attachment:fixed;</code>的效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170813_1158.gif" class="lozad post-image"src="http://img.smyhvae.com/20170813_1158.gif"></p>
<h3 id="background-综合属性"><a href="#background-综合属性" class="headerlink" title="background 综合属性"></a>background 综合属性</h3><p>background属性和border一样，是一个综合属性，可以将多个属性写在一起。(在<a target="_blank" rel="noopener" href="http://www.cnblogs.com/smyhvae/p/7256371.html">盒子模型</a>这篇文章中专门讲到border)</p>
<p>举例1:</p>
<pre class="highlight"><span class="line"><span class="attribute">background</span>:red <span class="built_in">url</span>(<span class="string">1.jpg</span>) no-repeat <span class="number">100px</span> <span class="number">100px</span> fixed;</span><br></pre>

<p>等价于：</p>
<pre class="highlight"><span class="line"><span class="attribute">background-color</span>:red;</span><br><span class="line"><span class="attribute">background-image</span>:<span class="built_in">url</span>(<span class="string">1.jpg</span>);</span><br><span class="line"><span class="attribute">background-repeat</span>:no-repeat;</span><br><span class="line"><span class="attribute">background-position</span>:<span class="number">100px</span> <span class="number">100px</span>;</span><br><span class="line"><span class="attribute">background-attachment</span>:fixed;</span><br></pre>

<p>以后，我们可以用小属性层叠掉大属性。</p>
<p>上面的属性中，可以任意省略其中的一部分。</p>
<p>比如说，对于下面这样的属性：</p>
<pre class="highlight"><span class="line"><span class="attribute">background</span>: blue <span class="built_in">url</span>(<span class="string">images/wuyifan.jpg</span>) no-repeat <span class="number">100px</span> <span class="number">100px</span>;</span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170813_1515.png" class="lozad post-image"src="http://img.smyhvae.com/20170813_1515.png"></p>
<h2 id="background-size属性：背景尺寸"><a href="#background-size属性：背景尺寸" class="headerlink" title="background-size属性：背景尺寸"></a><code>background-size</code>属性：背景尺寸</h2><p><code>background-size</code>属性：设置背景图片的尺寸。</p>
<p>格式举例：</p>
<pre class="highlight"><span class="line"><span class="comment">/* 宽、高的具体数值 */</span></span><br><span class="line">background-<span class="attr">size</span>: 500px 500px;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 宽高的百分比（相对于容器的大小） */</span></span><br><span class="line">background-<span class="attr">size</span>: <span class="number">50</span>% <span class="number">50</span>%;   <span class="comment">// 如果两个属性值相同，可以简写成：background-size: 50%;</span></span><br><span class="line"></span><br><span class="line">background-<span class="attr">size</span>: <span class="number">100</span>% auto;  <span class="comment">//这个属性可以自己试验一下。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* cover：图片始终填充满容器，且保证长宽比不变。图片如果有超出部分，则超出部分会被隐藏。 */</span></span><br><span class="line">background-<span class="attr">size</span>: cover;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* contain：将图片完整地显示在容器中，且保证长宽比不变。可能会导致容器的部分区域为空白。  */</span></span><br><span class="line">background-<span class="attr">size</span>: contain;</span><br></pre>

<p>这里我们对属性值 <code>cover</code> 和 <code>contain</code> 进行再次强调：</p>
<ul>
<li><p><code>cover</code>：图片始终<strong>填充满</strong>容器，且保证<strong>长宽比不变</strong>。图片如果有超出部分，则超出部分会被隐藏。</p>
</li>
<li><p><code>contain</code>：将图片<strong>完整地</strong>显示在容器中，且保证<strong>长宽比不变</strong>。可能会导致容器的部分区域留白。</p>
</li>
</ul>
<p>代码举例：（这张图片本身的尺寸是 1080 * 1350）</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.img_wrap</span> &#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.img</span> &#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">border</span>:<span class="number">1px</span> solid red;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background</span>: <span class="built_in">url</span>(<span class="string">http://img.smyhvae.com/20191006_1330.jpg</span>) no-repeat;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-right</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.div1</span> &#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-size</span>: cover;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.div2</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-size</span>: contain;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">	</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">section</span> <span class="attr">class</span>=<span class="string">&quot;img_wrap&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;img div1&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;img div2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;/<span class="name">section</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20191006_1350.png" class="lozad post-image"src="http://img.smyhvae.com/20191006_1350.png"></p>
<p>在上方代码的基础之上，再加一个 <code>background-position: center</code>属性之后，图片就会在容器里<strong>居中显示</strong>：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20191006_1520.png" class="lozad post-image"src="http://img.smyhvae.com/20191006_1520.png"></p>
<h2 id="背景原点：background-origin-属性"><a href="#背景原点：background-origin-属性" class="headerlink" title="背景原点：background-origin 属性"></a>背景原点：<code>background-origin</code> 属性</h2><p><code>background-origin</code> 属性：控制背景从什么地方开始显示。</p>
<p>格式举例：</p>
<pre class="highlight"><span class="line"></span><br><span class="line"><span class="comment">/* 从 padding-box 内边距开始显示背景图 */</span></span><br><span class="line">background-<span class="attr">origin</span>: padding-box;           <span class="comment">//默认值</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 从 border-box 边框开始显示背景图  */</span></span><br><span class="line">background-<span class="attr">origin</span>: border-box;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 从 content-box 内容区域开始显示背景图  */</span></span><br><span class="line">background-<span class="attr">origin</span>: content-box;</span><br><span class="line"></span><br></pre>

<p>如果属性值设置成了<code>border-box</code>，那边框部分也会显示图片哦。</p>
<p>如下图所示：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_2115.png" class="lozad post-image"src="http://img.smyhvae.com/20180207_2115.png"></p>
<h2 id="background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面"><a href="#background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面" class="headerlink" title="background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面"></a><code>background-clip</code>属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面</h2><p>格式举例：</p>
<p><code>background-clip: content-box;</code>   超出的部分，将裁剪掉。属性值可以是：</p>
<ul>
<li><p><code>border-box</code> 超出 border-box 的部分，将裁剪掉</p>
</li>
<li><p><code>padding-box</code> 超出 padding-box 的部分，将裁剪掉</p>
</li>
<li><p><code>content-box</code> 超出 content-box 的部分，将裁剪掉</p>
</li>
</ul>
<p>假设现在有这样的属性设置：</p>
<pre class="highlight"><span class="line">background-<span class="attr">origin</span>: border-box;</span><br><span class="line"></span><br><span class="line">background-<span class="attr">clip</span>: content-box;</span><br></pre>

<p>上方代码的意思是，背景图片从<strong>边框部分</strong>开始加载，但是呢，超出<strong>内容区域</strong>的部分将被裁减掉。</p>
<h2 id="同时设置多个背景"><a href="#同时设置多个背景" class="headerlink" title="同时设置多个背景"></a>同时设置多个背景</h2><p>我们可以给一个盒子同时设置多个背景，用以逗号隔开即可。可用于自适应局。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.box</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">416px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">100px</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="comment">/* 给盒子加多个背景，按照背景语法格式书写，多个背景使用逗号隔开 */</span></span></span><br><span class="line"><span class="language-css">            <span class="attribute">background</span>: <span class="built_in">url</span>(<span class="string">images/bg1.png</span>) no-repeat left top,</span></span><br><span class="line"><span class="language-css">            <span class="built_in">url</span>(<span class="string">images/bg2.png</span>) no-repeat right top,</span></span><br><span class="line"><span class="language-css">            <span class="built_in">url</span>(<span class="string">images/bg3.png</span>) no-repeat right bottom,</span></span><br><span class="line"><span class="language-css">            <span class="built_in">url</span>(<span class="string">images/bg4.png</span>) no-repeat left bottom,</span></span><br><span class="line"><span class="language-css">            <span class="built_in">url</span>(<span class="string">images/bg5.png</span>) no-repeat center;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>实现效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_2140.gif" class="lozad post-image"src="http://img.smyhvae.com/20180207_2140.gif"></p>
<p>上方代码中，我们其实给盒子设置了五张小图，拼成的一张大图。当改变浏览器窗口大小时，可以自适应布局。</p>
<h2 id="渐变：background-image"><a href="#渐变：background-image" class="headerlink" title="渐变：background-image"></a>渐变：background-image</h2><p>渐变是CSS3当中比较丰富多彩的一个特性，通过渐变我们可以实现许多炫丽的效果，有效的减少图片的使用数量，并且具有很强的适应性和可扩展性。</p>
<p>渐变分为：</p>
<ul>
<li><p>线性渐变：沿着某条直线朝一个方向产生渐变效果。</p>
</li>
<li><p>径向渐变：从一个<strong>中心点</strong>开始沿着<strong>四周</strong>产生渐变效果。</p>
</li>
<li><p>重复渐变。</p>
</li>
</ul>
<p>见下图：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180208_1140.png" class="lozad post-image"src="http://img.smyhvae.com/20180208_1140.png"></p>
<h3 id="线性渐变"><a href="#线性渐变" class="headerlink" title="线性渐变"></a>线性渐变</h3><p>格式：</p>
<pre class="highlight"><span class="line"></span><br><span class="line">background-<span class="attr">image</span>: linear-<span class="title function_">gradient</span>(方向, 起始颜色, 终止颜色);</span><br><span class="line"></span><br><span class="line">background-<span class="attr">image</span>: linear-<span class="title function_">gradient</span>(to right, yellow, green);</span><br></pre>

<p>参数解释：</p>
<ul>
<li>方向可以是：<code>to left</code>、<code>to right</code>、<code>to top</code>、<code>to bottom</code>、角度<code>30deg</code>（指的是顺时针方向30°）。</li>
</ul>
<p>格式举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">10px</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/* 语法：</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            linear-gradient(方向，起始颜色，终止颜色);</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            方向：to left   to right  to top   to bottom 　角度　30deg</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            起始颜色</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            终止颜色</span></span></span><br><span class="line"><span class="comment"><span class="language-css">        */</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(to right, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/* 不写方向，表示默认的方向是：从上往下 */</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">2</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/* 方向可以指定角度 */</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">3</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(<span class="number">135deg</span>, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/* 0%的位置开始出现黄色，40%的位置开始出现红色的过度。70%的位置开始出现绿色的过度，100%的位置开始出现蓝色 */</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">4</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(to right,</span></span><br><span class="line"><span class="language-css">            yellow <span class="number">0%</span>,</span></span><br><span class="line"><span class="language-css">            red <span class="number">40%</span>,</span></span><br><span class="line"><span class="language-css">            green <span class="number">70%</span>,</span></span><br><span class="line"><span class="language-css">            blue <span class="number">100%</span>);</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/* 颜色之间，出现突变 */</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">5</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(<span class="number">45deg</span>,</span></span><br><span class="line"><span class="language-css">            yellow <span class="number">0%</span>,</span></span><br><span class="line"><span class="language-css">            yellow <span class="number">25%</span>,</span></span><br><span class="line"><span class="language-css">            blue <span class="number">25%</span>,</span></span><br><span class="line"><span class="language-css">            blue <span class="number">50%</span>,</span></span><br><span class="line"><span class="language-css">            red <span class="number">50%</span>,</span></span><br><span class="line"><span class="language-css">            red <span class="number">75%</span>,</span></span><br><span class="line"><span class="language-css">            green <span class="number">75%</span>,</span></span><br><span class="line"><span class="language-css">            green <span class="number">100%</span></span></span><br><span class="line"><span class="language-css">            );</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">6</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(to right,</span></span><br><span class="line"><span class="language-css">            <span class="number">#000</span> <span class="number">0%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#000</span> <span class="number">25%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#fff</span> <span class="number">25%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#fff</span> <span class="number">50%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#000</span> <span class="number">50%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#000</span> <span class="number">75%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#fff</span> <span class="number">75%</span>,</span></span><br><span class="line"><span class="language-css">            <span class="number">#fff</span> <span class="number">100%</span></span></span><br><span class="line"><span class="language-css">            );</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_2222.png" class="lozad post-image"src="http://img.smyhvae.com/20180207_2222.png"></p>
<p><strong>举例</strong>：按钮</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>CSS3 渐变<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">html</span>, <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#f8fcd4</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.nav</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">800px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding-top</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*设置按钮基本样式*/</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.nav</span> <span class="selector-tag">a</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: inline-block;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">line-height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-size</span>: <span class="number">14px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-decoration</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#e59500</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#FFB700</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">linear-gradient</span>(</span></span><br><span class="line"><span class="language-css">                    to bottom,</span></span><br><span class="line"><span class="language-css">                    <span class="number">#FFB700</span> <span class="number">0%</span>,</span></span><br><span class="line"><span class="language-css">                    <span class="number">#FF8C00</span> <span class="number">100%</span></span></span><br><span class="line"><span class="language-css">            );</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;nav&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航1<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航2<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航3<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航4<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航5<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:;&quot;</span>&gt;</span>导航6<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_2301.png" class="lozad post-image"src="http://img.smyhvae.com/20180207_2301.png"></p>
<h3 id="径向渐变"><a href="#径向渐变" class="headerlink" title="径向渐变"></a>径向渐变</h3><p>格式：</p>
<pre class="highlight"><span class="line">background-image: radial-gradient(辐射的半径大小, 中心的位置, 起始颜色, 终止颜色);</span><br><span class="line"></span><br><span class="line">background-image: radial-gradient(100px at center,yellow ,green);</span><br><span class="line"></span><br></pre>

<p>解释：围绕中心点做渐变，半径是150px，从黄色到绿色做渐变。</p>
<p>中心点的位置可以是：at  left  right  center bottom  top。如果以像素为单位，则中心点参照的是盒子的左上角。</p>
<p>当然，还有其他的各种参数。格式举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">250px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">250px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            径向渐变：</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            radial-gradient（辐射的半径大小, 中心的位置，起始颜色，终止颜色）;</span></span></span><br><span class="line"><span class="comment"><span class="language-css">            中心点位置：at  left  right  center bottom  top</span></span></span><br><span class="line"><span class="comment"><span class="language-css">        */</span></span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*辐射半径为100px，中心点在中间*/</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(<span class="number">100px</span> at center, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*中心点在左上角*/</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">3</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(at left top, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">2</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(at <span class="number">50px</span> <span class="number">50px</span>, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*设置不同的颜色渐变*/</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">4</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(<span class="number">100px</span> at center,</span></span><br><span class="line"><span class="language-css">            yellow <span class="number">0%</span>,</span></span><br><span class="line"><span class="language-css">            green <span class="number">30%</span>,</span></span><br><span class="line"><span class="language-css">            blue <span class="number">60%</span>,</span></span><br><span class="line"><span class="language-css">            red <span class="number">100%</span>);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="comment">/*如果辐射半径的宽高不同，那就是椭圆*/</span></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">5</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(<span class="number">100px</span> <span class="number">50px</span> at center, yellow, green);</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180207_2256.png" class="lozad post-image"src="http://img.smyhvae.com/20180207_2256.png"></p>
<p><strong>举例：</strong>利用径向渐变和边框圆角的属性，生成按钮。代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>CSS3 渐变<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">40px</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border-radius</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: yellowgreen;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span><span class="selector-pseudo">:nth-child</span>(<span class="number">2</span>) &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">40px</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border-radius</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: yellowgreen;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">radial-gradient</span>(</span></span><br><span class="line"><span class="language-css">                    <span class="number">200px</span> at <span class="number">100px</span> <span class="number">100px</span>,</span></span><br><span class="line"><span class="language-css">                    <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>),</span></span><br><span class="line"><span class="language-css">                    <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.5</span>)</span></span><br><span class="line"><span class="language-css">            );</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180208_1133.png" class="lozad post-image"src="http://img.smyhvae.com/20180208_1133.png"></p>
<p>上图中，给第二个div设置的透明度是从0到0.5。如果设置的透明度是从0到0，则样式无变化，和第一个div一样。如果设置的透明度是从1到1，则盒子是全黑的。</p>
<h2 id="clip-path：裁剪出元素的部分区域做展示"><a href="#clip-path：裁剪出元素的部分区域做展示" class="headerlink" title="clip-path：裁剪出元素的部分区域做展示"></a>clip-path：裁剪出元素的部分区域做展示</h2><p><code>clip-path</code>属性可以创建一个只有元素的部分区域可以显示的剪切区域。区域内的部分显示，区域外的隐藏。</p>
<p>虽然<code>clip-path</code>不是背景属性，但这个属性非常强大，但往往会结合背景属性一起使用，达到一些效果。</p>
<p>举例：（鼠标悬停时，放大裁剪的区域）</p>
<pre class="highlight"><span class="line"><span class="selector-class">.div1</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">320px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">320px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">    <span class="attribute">background</span>: <span class="built_in">url</span>(<span class="string">http://img.smyhvae.com/20191006_1410.png</span>) no-repeat;</span><br><span class="line">    <span class="attribute">background-size</span>: cover;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 裁剪出圆形区域 */</span></span><br><span class="line">    <span class="attribute">clip-path</span>: <span class="built_in">circle</span>(<span class="number">50px</span> at <span class="number">100px</span> <span class="number">100px</span>);</span><br><span class="line">    <span class="attribute">transition</span>: clip-path .<span class="number">4s</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.div1</span><span class="selector-pseudo">:hover</span>&#123;</span><br><span class="line">    <span class="comment">/* 鼠标悬停时，裁剪出更大的圆形 */</span></span><br><span class="line">    <span class="attribute">clip-path</span>: <span class="built_in">circle</span>(<span class="number">80px</span> at <span class="number">100px</span> <span class="number">100px</span>);</span><br><span class="line">&#125;</span><br></pre>

<p><code>clip-path</code>属性的好处是，即使做了任何裁剪，<strong>容器的占位大小是不变的</strong>。比如上方代码中，容器的占位大小一直都是 320px * 320px。这样的话，也方便我们做一些动画效果。</p>
<p><code>clip-path: polygon()</code>举例：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20191006_1430.png" class="lozad post-image"src="http://img.smyhvae.com/20191006_1430.png"></p>
<p>另外，通过 <code>clip-path: (svg)</code> 可以导入svg矢量图，实现 iOS图标的圆角。这里就不详细展开了。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/02-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7/">
            https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/02-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/03-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E5%92%8C%E9%80%89%E6%8B%A9%E5%99%A8/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">03-CSS样式表和选择器 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/04-CSS%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E4%BC%AA%E7%B1%BB/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">04-CSS选择器：伪类 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#background-%E7%9A%84%E5%B8%B8%E8%A7%81%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7"><span class="toc-text">background 的常见背景属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-color%EF%BC%9A%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2%E7%9A%84%E8%A1%A8%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-text">background-color：背景颜色的表示方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%A8%E8%8B%B1%E8%AF%AD%E5%8D%95%E8%AF%8D%E8%A1%A8%E7%A4%BA"><span class="toc-text">用英语单词表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGB-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGB 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGBA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGBA 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">十六进制表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HSLA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">HSLA 表示法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-repeat%E5%B1%9E%E6%80%A7"><span class="toc-text">background-repeat属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-position%E5%B1%9E%E6%80%A7"><span class="toc-text">background-position属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-attachment-%E5%B1%9E%E6%80%A7"><span class="toc-text">background-attachment 属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#background-%E7%BB%BC%E5%90%88%E5%B1%9E%E6%80%A7"><span class="toc-text">background 综合属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-size%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B0%BA%E5%AF%B8"><span class="toc-text">background-size属性：背景尺寸</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%83%8C%E6%99%AF%E5%8E%9F%E7%82%B9%EF%BC%9Abackground-origin-%E5%B1%9E%E6%80%A7"><span class="toc-text">背景原点：background-origin 属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-clip%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E7%9A%84%E8%83%8C%E6%99%AF%EF%BC%88%E8%83%8C%E6%99%AF%E5%9B%BE%E7%89%87%E6%88%96%E9%A2%9C%E8%89%B2%EF%BC%89%E6%98%AF%E5%90%A6%E5%BB%B6%E4%BC%B8%E5%88%B0%E8%BE%B9%E6%A1%86%E4%B8%8B%E9%9D%A2"><span class="toc-text">background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E8%AE%BE%E7%BD%AE%E5%A4%9A%E4%B8%AA%E8%83%8C%E6%99%AF"><span class="toc-text">同时设置多个背景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B8%90%E5%8F%98%EF%BC%9Abackground-image"><span class="toc-text">渐变：background-image</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BA%BF%E6%80%A7%E6%B8%90%E5%8F%98"><span class="toc-text">线性渐变</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%84%E5%90%91%E6%B8%90%E5%8F%98"><span class="toc-text">径向渐变</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#clip-path%EF%BC%9A%E8%A3%81%E5%89%AA%E5%87%BA%E5%85%83%E7%B4%A0%E7%9A%84%E9%83%A8%E5%88%86%E5%8C%BA%E5%9F%9F%E5%81%9A%E5%B1%95%E7%A4%BA"><span class="toc-text">clip-path：裁剪出元素的部分区域做展示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#background-%E7%9A%84%E5%B8%B8%E8%A7%81%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7"><span class="toc-text">background 的常见背景属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-color%EF%BC%9A%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2%E7%9A%84%E8%A1%A8%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-text">background-color：背景颜色的表示方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%A8%E8%8B%B1%E8%AF%AD%E5%8D%95%E8%AF%8D%E8%A1%A8%E7%A4%BA"><span class="toc-text">用英语单词表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGB-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGB 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#RGBA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">RGBA 表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">十六进制表示法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HSLA-%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-text">HSLA 表示法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-repeat%E5%B1%9E%E6%80%A7"><span class="toc-text">background-repeat属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-position%E5%B1%9E%E6%80%A7"><span class="toc-text">background-position属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-attachment-%E5%B1%9E%E6%80%A7"><span class="toc-text">background-attachment 属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#background-%E7%BB%BC%E5%90%88%E5%B1%9E%E6%80%A7"><span class="toc-text">background 综合属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-size%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B0%BA%E5%AF%B8"><span class="toc-text">background-size属性：背景尺寸</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%83%8C%E6%99%AF%E5%8E%9F%E7%82%B9%EF%BC%9Abackground-origin-%E5%B1%9E%E6%80%A7"><span class="toc-text">背景原点：background-origin 属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#background-clip%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E7%9A%84%E8%83%8C%E6%99%AF%EF%BC%88%E8%83%8C%E6%99%AF%E5%9B%BE%E7%89%87%E6%88%96%E9%A2%9C%E8%89%B2%EF%BC%89%E6%98%AF%E5%90%A6%E5%BB%B6%E4%BC%B8%E5%88%B0%E8%BE%B9%E6%A1%86%E4%B8%8B%E9%9D%A2"><span class="toc-text">background-clip属性：设置元素的背景（背景图片或颜色）是否延伸到边框下面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E8%AE%BE%E7%BD%AE%E5%A4%9A%E4%B8%AA%E8%83%8C%E6%99%AF"><span class="toc-text">同时设置多个背景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B8%90%E5%8F%98%EF%BC%9Abackground-image"><span class="toc-text">渐变：background-image</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BA%BF%E6%80%A7%E6%B8%90%E5%8F%98"><span class="toc-text">线性渐变</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%84%E5%90%91%E6%B8%90%E5%8F%98"><span class="toc-text">径向渐变</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#clip-path%EF%BC%9A%E8%A3%81%E5%89%AA%E5%87%BA%E5%85%83%E7%B4%A0%E7%9A%84%E9%83%A8%E5%88%86%E5%8C%BA%E5%9F%9F%E5%81%9A%E5%B1%95%E7%A4%BA"><span class="toc-text">clip-path：裁剪出元素的部分区域做展示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
